package com.atguigu.chapter07.d_state;

import com.atguigu.chapter5.source.WaterSensor;
import org.apache.commons.codec.language.bm.Languages;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.state.*;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @ClassName: Flink01_State_Operator_List
 * @Description:
 * @Author: kele
 * @Date: 2021/4/7 19:38
 *
 *
 * 键控状态之 valueState
 *
 * 通过runtime获取
 *
 * 案例：检测传感器的水位值，如果连续的两个水位值超过10，就输出报警。
 *
 * 1、先重写open方法，获取状态
 *      通过state = getRuntimeContext().getState(new ValueStateDescriptor<>())获取单值的状态
 * 2、数据处理更新状态
 *      state.value -- 取出状态中的值
 *      state.update --更新状态
 *
 *
 *
 *
 **/
public class Flink03_Key_State_ValueState {

    public static void main(String[] args) {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port",20000);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        env.socketTextStream("hadoop162",8888)
                .map(datas->{

                    String[] data = datas.split(",");
                    return new WaterSensor(data[0],
                            Long.parseLong(data[1]) * 1000,
                            Integer.valueOf(data[2]));
                })
                .keyBy(WaterSensor::getId)
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {

                    private ValueState<Integer> state;
                    private Integer lastVC;

                    @Override
                    public void open(Configuration parameters) throws Exception {

                        state = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("valuestate", Integer.class));
                    }

                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                        lastVC = state.value();

                        //如果不是第一次 且 连续两次水位高于10，报警
                        if(lastVC != null && lastVC >= 10 && value.getVc() > 10){
                            out.collect("连续两次水位高于10，报警.....");
                        }

                        //将这次的水位值保存为状态
                        state.update(value.getVc());


                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
